package demo1;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-03-06
 * Time: 01:03
 */
class Counter{
    public int flag = 0;
}

public class demo3_synchronized {
    public static void main(String[] args) {
        Counter counter = new Counter();
        
        Thread thread1 = new Thread(()->{
            while (true){
                synchronized (counter){
                    if (counter.flag != 0){
                        break;
                    }
                }
            }
            System.out.println("Over thread1");
        });
        Thread thread2 = new Thread(()->{
            Scanner in = new Scanner(System.in);
            counter.flag = in.nextInt();
        });

        thread1.start();
        thread2.start();
    }
}
